﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TreeView;

namespace Bank_database_system.业务部
{
    public partial class 保险箱租用a : UserControl
    {
        private string branch_id = "";
        private string staff_id = "";
        private string BRANCH_TEXT = "";
        private string BOX_SIZE_TEXT = "";
        private string STATE_TEXT = "";
        private string BOX_ID_TEXT = "";
        // 定义一个全局变量来存储 SAFE_BOX_ID
        private string selectedSafeBoxId;
        保险箱租用b rent_safebox2 = new 保险箱租用b();
        保险箱租用c rent_safebox3 = new 保险箱租用c();
        // 定义数据库连接字符串作为常量
        public const string connectionString =
            @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=47.96.39.153)(PORT=1521))
            (CONNECT_DATA=(SID=orcl)));User Id=system;Password=Tongji123456;";

        int page = 1;
        int totalRecordCount = 1;

        public event EventHandler ButtonBack;
        public 保险箱租用a()
        {
            InitializeComponent();
            //totalRecordCount = GetTotalRecordCount();

            rent_safebox2.ButtonBack += BackToA;
            rent_safebox3.ButtonBack += BackToA;

            // 绑定 DataGridView 的 CellClick 事件
            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(this.dataGridView1_CellClick);
            totalRecordCount = Safe_Box_Operations.GetSafeBoxCount(BRANCH_TEXT, BOX_SIZE_TEXT, STATE_TEXT, BOX_ID_TEXT);
            // 调用 Safe_Box_Operations 并将结果绑定到 dataGridView1
            LoadSafeBoxData();
        }

        public void branch_and_staff_get(string branchID, string staffID)
        {
            branch_id = branchID;
            staff_id = staffID;
        }

        private int GetTotalRecordCount()
        {
            int count = 0;
            string query = "SELECT COUNT(*) FROM SAFE_BOX";

            try
            {
                using (OracleConnection connection = new OracleConnection(connectionString))
                {
                    connection.Open();

                    using (OracleCommand command = new OracleCommand(query, connection))
                    {
                        count = Convert.ToInt32(command.ExecuteScalar());
                    }
                }
            }
            catch (OracleException ex)
            {
                MessageBox.Show($"数据库操作失败：{ex.Message}", "数据库错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return count;
        }

        public void LoadSafeBoxData()
        {
            // 调用  InquirySafeBox函数
            dataGridView1.DataSource =
                Safe_Box_Operations.InquirySafeBox(page.ToString(), "", "", "", "");
            label9.Text = "当前第  "+$"{page}"+"  页        共有  "+$"{(totalRecordCount - 1) / 10 + 1}  页 ";
            // 设置每一列
            SetDataGridViewColumnWidthsAndRowHeights();
        }

        private void SetDataGridViewColumnWidthsAndRowHeights()
        {
            if (dataGridView1.Rows.Count == 0)
            {
                // 如果 dataGridView1 没有任何行数据，则将 DataSource 设为 null
                dataGridView1.DataSource = null;
                // 如果没有行数据，隐藏“详情”列
                dataGridView1.Columns["DetailsButton"].Visible = false;
            }
            else
            {
                
                // 添加“详情”按钮列
                if (!dataGridView1.Columns.Contains("DetailsButton"))
                {
                    DataGridViewButtonColumn detailsButtonColumn = new DataGridViewButtonColumn();
                    detailsButtonColumn.Name = "DetailsButton";
                    detailsButtonColumn.HeaderText = "详细信息";
                    detailsButtonColumn.Text = "详情";
                    detailsButtonColumn.UseColumnTextForButtonValue = true;
                    dataGridView1.Columns.Add(detailsButtonColumn);
                    
                }
                dataGridView1.Columns["DetailsButton"].Visible = true;
                // 将“详情”列移到最后一列
                dataGridView1.Columns["DetailsButton"].DisplayIndex = dataGridView1.Columns.Count - 1;
                // 设置指定列的标题文本
                dataGridView1.Columns["SAFE_BOX_ID"].HeaderText = "保险箱ID";
                dataGridView1.Columns["BRANCH_ID"].HeaderText = "支行ID";
                dataGridView1.Columns["BOX_SIZE"].HeaderText = "保险箱大小";
                dataGridView1.Columns["STATE"].HeaderText = "保险箱状态";
                dataGridView1.Columns["RNUM"].Visible = false;
                dataGridView1.RowHeadersVisible = false;
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AllowUserToResizeColumns = false;
                dataGridView1.AllowUserToResizeRows = false;
                dataGridView1.ReadOnly = true;

                

                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                foreach (DataGridViewColumn column in dataGridView1.Columns)
                {
                    column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
            }

        }
        //详情
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 检查点击的是否是按钮列
            if (e.ColumnIndex == dataGridView1.Columns["DetailsButton"].Index && e.RowIndex >= 0)
            {
                // 获取选中行的 SAFE_BOX_ID
                string selectedSafeBoxId = dataGridView1.Rows[e.RowIndex].Cells["SAFE_BOX_ID"].Value.ToString();
                //切换到下一页面
                panel1.Visible = true;
                panel1.Controls.Clear();
                rent_safebox2.CardID_Set(textBox5.Text, Safe_Box_Operations.SelectSafeBox(selectedSafeBoxId));
                rent_safebox2.branch_and_staff_get(branch_id, staff_id);
                panel1.Controls.Add(rent_safebox2);
                panel1.BringToFront();//显示置顶
            }
        }

        private void BackToA(object sender, EventArgs e)
        {
            // 修改柜台业务a中的控件状态
            panel1.Visible = false;
            panel1.Controls.Clear();
            LoadSafeBoxData();
        }
        public void CardID_Set(string cardID)
        {
            textBox5.Text = cardID;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            comboBox1.Text = " ";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            ButtonBack?.Invoke(this, EventArgs.Empty);
        }


        //重置
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            comboBox1.Text ="";
            textBox3.Text = "";
            textBox4.Text = "";
            comboBox1.SelectedIndex = -1; // 清空 ComboBox1 的选中项
            BRANCH_TEXT = "";
            BOX_SIZE_TEXT = "";
            STATE_TEXT = "";
            BOX_ID_TEXT = "";
            //调用筛选以重置
            // 调用 Safe_Box_Operations 并将结果绑定到 dataGridView1
            totalRecordCount = Safe_Box_Operations.GetSafeBoxCount(BRANCH_TEXT, BOX_SIZE_TEXT, STATE_TEXT, BOX_ID_TEXT);
            page = 1;
            LoadSafeBoxData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BRANCH_TEXT = textBox4.Text;
            BOX_SIZE_TEXT = textBox1.Text;
            STATE_TEXT = comboBox1.Text;
            BOX_ID_TEXT = textBox3.Text;
           //筛选作用
           totalRecordCount = Safe_Box_Operations.GetSafeBoxCount(BRANCH_TEXT, BOX_SIZE_TEXT, STATE_TEXT, BOX_ID_TEXT);
            page = 1;
            // 将返回的 DataTable 绑定到 dataGridView1
            dataGridView1.DataSource =
                Safe_Box_Operations.InquirySafeBox(page.ToString(), BRANCH_TEXT, BOX_SIZE_TEXT, STATE_TEXT, BOX_ID_TEXT);
            // 设置每一列
            label9.Text = "当前第  " + $"{page}" + "  页        共有  " + $"{(totalRecordCount - 1) / 10 + 1}  页 ";
            SetDataGridViewColumnWidthsAndRowHeights();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //上一页
            if (page == 1)
            {
                return;
            }
            else
            {
                page--;
                dataGridView1.DataSource =
                Safe_Box_Operations.InquirySafeBox(page.ToString(), BRANCH_TEXT, BOX_SIZE_TEXT, STATE_TEXT, BOX_ID_TEXT);
                // 设置每一列
                label9.Text = "当前第  " + $"{page}" + "  页        共有  " + $"{(totalRecordCount - 1) / 10 + 1}  页 ";
                SetDataGridViewColumnWidthsAndRowHeights();
            }
        }
        private void button13_Click(object sender, EventArgs e)
        {
            //下一页
            if (page == (totalRecordCount - 1) / 10 + 1)
            {
                return;
            }
            else
            {
                page++;
                dataGridView1.DataSource =
                Safe_Box_Operations.InquirySafeBox(page.ToString(), BRANCH_TEXT, BOX_SIZE_TEXT, STATE_TEXT, BOX_ID_TEXT);
                // 设置每一列
                label9.Text = "当前第  " + $"{page}" + "  页        共有  " + $"{(totalRecordCount - 1) / 10 + 1}  页 ";
                SetDataGridViewColumnWidthsAndRowHeights();
            }
        }


        //个人保险箱
        private void button0_Click(object sender, EventArgs e)
        {
            //切换到下一页面
            panel1.Visible = true;
            panel1.Controls.Clear();
            rent_safebox3.CardID_Set(textBox5.Text);
            rent_safebox3.branch_and_staff_get(branch_id, staff_id);
            rent_safebox3.LoadSafeBoxData(textBox5.Text);
            panel1.Controls.Add(rent_safebox3);
            panel1.BringToFront();//显示置顶
        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}